<?php
/**
 * Create By: PhpStorm
 * User: yuanxuncheng
 * File: GetOrder.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\order;

use app\api\model\dao\Comment;
use app\api\model\dao\Goods;
use app\api\model\dao\OrderGoods;
use app\api\model\dao\OrderInfo;
use app\api\model\dao\Region;
use app\library\Error;
use app\library\PageBase;
use think\Validate;
use think\Cache;
use app\library\Flag;

class GetOrder extends PageBase
{

    private $objOrderInfo;
    private $objOrderGoods;
    private $objGoods;
    private $objRegion;
    private $objComment;

    public function __construct()
    {
        parent::__construct();
        $this->objOrderInfo = new OrderInfo();
        $this->objOrderGoods = new OrderGoods();
        $this->objGoods = new Goods();
        $this->objRegion = new Region();
        $this->objComment = new Comment();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'status' => 'require',
        ];
        $msg = [
            'status.require' => '订单状态必填',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    public function excute($param)
    {
        $ret = $this->checkToken($param);
        if (false === $ret) {
            return $this->getRes(Error::ERR_NO_AUTH);
        }

        // TODO:业务逻辑
        // 1、参数校验
        $ret = $this->checkParam($param);
        if ($ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }

        // 获取订单信息
        $arrCond = array();
        $arrCond['user_id'] = $this->user_id;
        $arrCond['order_status'] = ['<>', Flag::ORDER_STATUS_DELETE];

        if ($param['status'] == 0) {

        }

        if ($param['status'] == 1) {
            $arrCond['order_status'] = ['=', Flag::ORDER_STATUS_NOT_PAY];
        }

        if ($param['status'] == 2) {
            $arrCond['order_status'] = ['=', Flag::ORDER_STATUS_NOT_SENDING];
        }

        if ($param['status'] == 3) {
            $arrCond['order_status'] = ['=', Flag::ORDER_STATUS_SENDING];
        }

        if ($param['status'] == 4) {
            $goods_id_arr = [0];
            $arrCommentConds = [
                'user_id' => ['=', $this->user_id],
                'comment_type' => ['=', 0],
            ];
            $arrCommentFields = ['id_value'];
            $comments = $this->objComment->getCommentByCond($arrCommentConds, $arrCommentFields);
            if (!empty($comments)) {
                foreach ($comments as $comment) {
                    $goods_id_arr[] = $comment['id_value'];
                }
            }

            $goods_ids = implode(',', $goods_id_arr);
            $arrCond['order_id'] = ['not in', $goods_ids];
            $arrCond['order_status'] = ['=', Flag::ORDER_STATUS_FINISH];
        }

        $arrCond['order_type'] = ['=', Flag::ORDER_TYPE_SHANGCHENG];

        $perpage = isset($param['perpage']) ? intval($param['perpage']) : 0;
        $page = isset($param['page']) ? intval($param['page']) : 0;
        $perpage = max(10, $perpage);
        $page = max(1, $page);
        $offset = ($page - 1) * $perpage;
        $limit = $perpage;

        $counts = $this->objOrderInfo->getCount($arrCond);
        $orderInfos = $this->objOrderInfo->getOrderByCond($arrCond, [], $offset, $limit, ' add_time desc ');
        if (false === $orderInfos) {
            return $this->getRes(Error::ERR_SYS);
        }

        foreach ($orderInfos as &$orderInfo) {
            $regionids = [];
            $regionids[] = $orderInfo['country'];
            unset($orderInfo['country']);

            $regionids[] = $orderInfo['province'];
            unset($orderInfo['province']);

            $regionids[] = $orderInfo['city'];
            unset($orderInfo['city']);

            $regionids[] = $orderInfo['district'];
            unset($orderInfo['district']);

            $orderInfo['order_status_desc'] = Flag::$map_order[$orderInfo['order_status']];
            $orderInfo['shipping_status_desc'] = Flag::$map_shipping[$orderInfo['shipping_status']];
            $orderInfo['pay_status_desc'] = Flag::$map_pay[$orderInfo['pay_status']];

            // 填补地址信息
            $regions = $this->objRegion->getRegion(implode(',', $regionids));
            if (!empty($regions)) {
                foreach ($regions as $region) {
                    if ($region['region_type'] === 0) {
                        $orderInfo['country'] = $region['region_name'];
                    }
                    if ($region['region_type'] === 1) {
                        $orderInfo['province'] = $region['region_name'];
                    }
                    if ($region['region_type'] === 2) {
                        $orderInfo['city'] = $region['region_name'];
                    }
                    if ($region['region_type'] === 3) {
                        $orderInfo['district'] = $region['region_name'];
                    }
                }
            }

            // 获取商品
            $goods_ids = [];
            $goods_arr = [];

            // 获取商品信息
            $orderGoods = $this->objOrderGoods->getOrderGoods($orderInfo['order_id']);
            $goods_ids = array_map(function ($v) {
                return $v['goods_id'];
            }, $orderGoods);
            $goods = $this->objGoods->getGoods(implode(',', $goods_ids));
            foreach ($goods as $good) {
                $goods_arr[$good['goods_id']] = $good;
            }

            foreach ($orderGoods as &$v) {
                if (isset($goods_arr[$v['goods_id']])) {
                    $v['market_price'] = $goods_arr[$v['goods_id']]['market_price'];
                    $v['shop_price'] = $goods_arr[$v['goods_id']]['shop_price'];
                    $v['sale_count'] = $goods_arr[$v['goods_id']]['sale_count'];
                    // 加上折扣
                    $v['discount'] = '' . round($goods_arr[$v['goods_id']]['shop_price'] / $goods_arr[$v['goods_id']]['market_price'], 1) * 10;
                    $v['goods_thumb'] = $goods_arr[$v['goods_id']]['goods_thumb'];
                }
            }
            $orderInfo['goods'] = $orderGoods;
        }

        $this->data = array(
            'list' => $orderInfos,
            'total' => $counts,
        );

        return $this->getRes();
    }
}
